//
//  CollectionTableViewController.m
//  OneDay
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 oneDay. All rights reserved.
//

#import "CollectionTableViewController.h"
#import "CollectionTableViewCell.h"
#import "PartyDetailViewController.h"
#import "ArticleDetailViewController.h"
#import "StyleNextViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "PartyModel.h"
#import "VideoModel.h"

@implementation CollectionTableViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 没有数据的时候为白色，没有线条
    self.tableView.separatorStyle = NO;
   
    
    UIBarButtonItem *letfButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-reply-3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(doBack)];
    self.navigationItem.leftBarButtonItem = letfButton;
    
    UIImageView *view = [[UIImageView alloc] initWithFrame:self.view.bounds];
    view.image = [UIImage imageNamed:@"article.jpg"];
    view.alpha = 0.4;
    self.tableView.backgroundView = view;
    
    LeanCloudObject *leanCloud = [[LeanCloudObject alloc] init];
    NSString *userName = [[DataManager shareManager] userName];
    NSString *style;
    if (self.currentIndex == 1) {
        style = @"party";
    }else if (self.currentIndex == 2){
        style = @"video";
    }else if (self.currentIndex == 3){
        style = @"web";
    }else{
        style = @"article";
    }
    self.allDataArray = (NSMutableArray *)[leanCloud selectStundetWithStyle:style userName:userName];
    
}

- (void)doBack
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}



#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.currentIndex == 1 || self.currentIndex == 2 || self.currentIndex == 3) {
        CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectionCell"];
        
        if (cell == nil) {
            cell = [[CollectionTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"collectionCell"];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLable.numberOfLines = 0;
        
        if (self.currentIndex == 1) {
            PartyObject *party = self.allDataArray[indexPath.row];
            [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:party.imageUrl] placeholderImage:[UIImage imageNamed:@"title.jpg"]];
            cell.titleLable.text = party.title;
        }else if (self.currentIndex == 2){
            VideoObject *video = self.allDataArray[indexPath.row];
            [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:video.imageUrl] placeholderImage:[UIImage imageNamed:@"title.jpg"]];
            cell.titleLable.text = video.title;
        }else{
            WebObject *web = self.allDataArray[indexPath.row];
            [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:web.imageUrl] placeholderImage:[UIImage imageNamed:@"title.jpg"]];
            cell.titleLable.text = web.title;
        }
        
        return cell;
    } else {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collection"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"collection"];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.numberOfLines = 0;
        
        ArticleObject *article = self.allDataArray[indexPath.row];
        cell.textLabel.text = article.title;
        
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 105;
}

//- (void)setEditing:(BOOL)editing animated:(BOOL)animated
//{
//    [self.tableView setEditing:editing animated:YES];
//}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        LeanCloudObject *leanCloud = [[LeanCloudObject alloc] init];
        NSString *userName = [[DataManager shareManager] userName];
        if (self.currentIndex == 1) {
            PartyObject *party = self.allDataArray[indexPath.row];
            [leanCloud deleteStudentObjectWithStyle:@"party" userName:userName title:party.title];
            self.allDataArray = (NSMutableArray *)[leanCloud selectStundetWithStyle:@"party" userName:userName];
        } else if (self.currentIndex == 2) {
            VideoObject *video = self.allDataArray[indexPath.row];
            [leanCloud deleteStudentObjectWithStyle:@"video" userName:userName title:video.title];
            self.allDataArray = (NSMutableArray *)[leanCloud selectStundetWithStyle:@"video" userName:userName];
        } else if (self.currentIndex == 3) {
            WebObject *web = self.allDataArray[indexPath.row];
            [leanCloud deleteStudentObjectWithStyle:@"web" userName:userName title:web.title];
            self.allDataArray = (NSMutableArray *)[leanCloud selectStundetWithStyle:@"web" userName:userName];
        } else if (self.currentIndex == 4) {
            ArticleObject *article = self.allDataArray[indexPath.row];
            [leanCloud deleteStudentObjectWithStyle:@"article" userName:userName title:article.title];
            self.allDataArray = (NSMutableArray *)[leanCloud selectStundetWithStyle:@"article" userName:userName];
        }
        [self.tableView reloadData];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.currentIndex == 1) {
        PartyDetailViewController *partyVC = [[PartyDetailViewController alloc] init];
        PartyObject *party = self.allDataArray[indexPath.row];
        partyVC.leo_id = party.needStr;
        [self.navigationController pushViewController:partyVC animated:YES];
        
    } else if (self.currentIndex == 2) {
        VideoObject *video = self.allDataArray[indexPath.row];
        // 播放路径
        NSURL *url = [NSURL URLWithString:video.needStr];
        AVPlayer *player = [AVPlayer playerWithURL:url];
        AVPlayerViewController *playerVC = [AVPlayerViewController new];
        playerVC.player = player;
        [self presentViewController:playerVC animated:YES completion:nil];
        
    } else if (self.currentIndex == 3) {
        StyleNextViewController *styleVC = [[StyleNextViewController alloc] init];
        WebObject *web = self.allDataArray[indexPath.row];
        styleVC.webUrl = web.needStr;
        [self.navigationController pushViewController:styleVC animated:YES];
    } else if (self.currentIndex == 4) {
        ArticleDetailViewController *articleVC = [[ArticleDetailViewController alloc] init];
        ArticleObject *article = self.allDataArray[indexPath.row];
        articleVC.needID = article.needStr;
        [self.navigationController pushViewController:articleVC animated:YES];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
